const { randomBytes } = require('crypto');
const secp256k1 = require('secp256k1');
// or require('secp256k1/elliptic')
//   if you want to use pure js implementation in node

// generate message to sign
const msg = randomBytes(32);//随机生成一个数据
console.log(msg.toString('hex'));

// generate privKey
let privKey = randomBytes(32)
console.log(privKey.toString('hex'));
// do {
//   privKey = randomBytes(32);//随机生成一个私钥
//   console.log(privKey.toString('hex'));

// } while (!secp256k1.privateKeyVerify(privKey));

// get the public key in a compressed format
const pubKey = secp256k1.publicKeyCreate(privKey);//根据私钥生成公钥
console.log(Buffer.from(pubKey).toString('hex'));
// sign the message
const sigObj = secp256k1.ecdsaSign(msg, privKey);//然后进行签名
console.log(Buffer.from(sigObj.signature).toString('hex'));
// verify the signature
console.log(secp256k1.ecdsaVerify( sigObj.signature,msg, pubKey));//核查签名是否正确
// => true
